﻿using JWTAuthServer.Model;
using JWTAuthServer.Utility;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Data.Common;
using System.Diagnostics;

namespace JWTAuthServer.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class AuthenticationController : ControllerBase
    {
        private readonly ILogger _logger;
        private readonly IJWTService _iJWTService;
        public AuthenticationController(ILogger<AuthenticationController> logger, IJWTService jwtService)
        {
            _logger = logger;
            _iJWTService = jwtService; 
        } 
        [Route("Login")]
        [HttpPost]
        public string Login(string name,string password)
        {
            //{
            //    //do 数据库校验
            //    for (int i = 0; i < 1000; i++)
            //    {
            //        Thread th = new Thread(new ThreadStart(ThreadMethod)); //创建线程                     
            //        th.Start(); //启动线程  
            //    }
            //}
            if ("admin".Equals(name) && "123456".Equals(password))
            {
                string token = this._iJWTService.GetToken(name, password);

                var userInfo = new
                {
                    id=10,
                    name = name,
                    password= password,
                    token=token
                };

                return JsonConvert.SerializeObject(new TResponse<object>() { data = userInfo, code = 200 });
            }
            else
            { 
                return JsonConvert.SerializeObject(new TResponse() { message = "登录失败，请确认账号或密码是否正确", code=400});
            }
        }
        [Route("LogOut")]
        [HttpPost]
        public string LogOut(string token="")
        { 
           return JsonConvert.SerializeObject(new { data = "退出成功", code = 200 });          
        }
    }
}
